<?php
	function habilitation()
	{
		$hab = array(
			1,
			2
		);
		
		if (in_array(get_s('bo_user_role'), $hab))
			return true;
		return false;
	}
	
	function edit($table, $id)
	{
		if (habilitation())
		{
			$message = '';
			$champs = champs();
			foreach ($champs as $champ)
			{
				$val = $_POST[$champ["id"]];
				if ($champ["obligatoire"] && !strlen($val))
					$message .= 'Le champ <strong>' . trad($champ["Affichage"], APPLNG) . '</strong> est vide mais obligatoire.<br />';
			}
			
			if (strlen($message))
			{
				$retour["statut"] = false;
				$retour["message"] = $message;
			}
			else
			{
				$gp_fonc = GP_fonctions::getInstance();
				$gp_db = GP_db::getInstance(BDD_DSN, BDD_UTILISATEUR, BDD_MOT_DE_PASSE);
				GP_ActiveRecord::setDefaultDBConnection($gp_db);
				
				$q = "SELECT name FROM $table WHERE language = '".addslashes($_POST["language"])."' && name = '".addslashes($_POST["name"])."'";
				$res = $gp_db -> fetchArray($q);
				
				if (count($res))
				{
					$retour["statut"] = false;
					$retour["message"] = trad('Ce pays existe déjà dans la base de données.', APPLNG);
				}
				else
				{
					$now = date("Y-m-d H:i:s");
					$add = new GP_ActiveRecord($table, $id);
					$add -> datemodif = $now;
					foreach ($champs as $champ)
					{
						$val = $_POST[$champ["id"]];
						if ($champ["type"] == 'password')
							$val = md5($val);
						$add -> $champ["id"] = $val;
					}
					
					$add -> soumettre();
					
					$retour["statut"] = true;
				}
			}
			return $retour;
		}
		return false;
	}
	
	function champs()
	{
		$retour = array();
		
		$retour['language']['id'] = 'language';
		$retour['language']['Affichage'] = 'Langue';
		$retour['language']['type'] = 'select';
		$retour['language']['typeselect'] = 'vocabulaire';
		$retour['language']['data'] = 'en%%en;fr%%fr;es%%es;de%%de';
		$retour['language']['premierchoix'] = trad('Choisir', APPLNG) . '%%';
		$retour['language']['defaut'] = '';
		$retour['language']['js'] = 'return false;';
		$retour['language']['obligatoire'] = true;
		
		$retour['name']['id'] = 'name';
		$retour['name']['Affichage'] = 'Nom';
		$retour['name']['max'] = 60;
		$retour['name']['type'] = 'input';
		$retour['name']['obligatoire'] = true;

		return $retour;
	}
	
	function datamaj($table, $id)
	{
		$gp_db = GP_db::getInstance(BDD_DSN, BDD_UTILISATEUR, BDD_MOT_DE_PASSE);
		$sql = "SELECT * FROM $table WHERE id = $id";
		$tab = $gp_db -> fetchArray_one($sql);
		return $tab;
	}
	
	$message_ok = false;
	if ($this -> action_form == 1)
	{
		$form = edit($this -> table, $this -> id);
		$this -> setvar('datamaj', $_POST);
		if (!$form["statut"])
		{
			$message_erreur = $form["message"];
			$this -> setvar('message_erreur', $message_erreur);
		}
		else
			$message_ok = true;
	}
	else
		$this -> setvar('datamaj', datamaj($this -> table, $this -> id));
	
	$this -> setvar('message_ok', $message_ok);
	$this -> setvar('titre', 'Editer un pays');
	$this -> setvar('retour_url', rw('echantillons'));
	$this -> setvar('habilitation', habilitation());
	$this -> setvar('champs', champs());
?>